package com.mwx.loseweight.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mwx.loseweight.common.result.Result;
import com.mwx.loseweight.common.utils.LoginUserUtils;
import com.mwx.loseweight.pojo.dto.FitWeightHisPageDTO;
import com.mwx.loseweight.pojo.entity.FitWeightHis;
import com.mwx.loseweight.pojo.entity.User;
import com.mwx.loseweight.service.FitWeightHisService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;

/**
 * @author Luruichuan
 */
@RestController
@RequestMapping("/weighthis")
@Tag(name = "体重历史记录")
public class FitWeightHisController {
	
	@Autowired
	private FitWeightHisService fitWeightHisService;
	@Autowired
	private LoginUserUtils loginUserUtils;
	
	@PostMapping("/addnew")
    @Operation(summary = "新增体重历史记录")
    public Result<?> addnew(@RequestBody FitWeightHis fitWeightHisDTO, HttpServletRequest request) {
		User user = loginUserUtils.getLoginUser(request);
		if(user == null) return Result.error("无法获取登录用户");
		fitWeightHisDTO.setCreateBy(user.getCreateBy());
		fitWeightHisService.save(fitWeightHisDTO);
		return Result.success();
    }
	
	@PostMapping("/delete")
    @Operation(summary = "删除体重历史记录")
    public Result<?> deleteUser(@RequestBody FitWeightHis fitWeightHis, HttpServletRequest request) {
		User user = loginUserUtils.getLoginUser(request);
		if(user == null) return Result.error("无法获取登录用户");
		fitWeightHis.setUpdateBy(user.getCreateBy());
		fitWeightHisService.delete(fitWeightHis);
        return Result.success();
    }
	
	@PostMapping("/update")
    @Operation(summary = "更新体重历史记录")
    public Result<?> update(@RequestBody FitWeightHis fitWeightHis, HttpServletRequest request) {
		User user = loginUserUtils.getLoginUser(request);
		if(user == null) return Result.error("无法获取登录用户");
		fitWeightHis.setUpdateBy(user.getCreateBy());
		fitWeightHisService.update(fitWeightHis);
        return Result.success();
    }
	
	@PostMapping("/get")
    @Operation(summary = "获取用户详情")
    public Result<FitWeightHis> getFitWeightHis(@RequestBody FitWeightHis fitWeightHis) {
		FitWeightHis savedFitWeightHis = fitWeightHisService.get(fitWeightHis);
        return Result.success(savedFitWeightHis);
    }
	
	@PostMapping("/list")
    @Operation(summary = "获取体重历史记录")
    public Result<Page<FitWeightHis>> getFitWeightHisList(@RequestBody FitWeightHisPageDTO fitWeightHisPageDTO) {
        Page<FitWeightHis> pageResult = fitWeightHisService.list(fitWeightHisPageDTO);
        return Result.success(pageResult);
    }
	
}





























